package com.kanshu.basictest.widget.adapter;

import android.content.Context;

import androidx.annotation.NonNull;

import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseListAdapter<T> extends BaseAdapter {

    private List<T> mDataSource;
    private Context mContext;

    public BaseListAdapter(@NonNull Context context, List<T> dataSource) {
        mDataSource = dataSource == null ? new ArrayList<T>() : dataSource;
        mContext = context;
    }

    @Override
    public int getCount() {
        return mDataSource.size();
    }

    @Override
    public T getItem(int i) {
        return mDataSource.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }


    public Context getContext() {
        return mContext;
    }

    public List<T> getDataSource() {
        return mDataSource;
    }

    public void setDataSource(List<T> dataSource) {
        mDataSource = dataSource;
        notifyDataSetChanged();
    }

    public void clearAll() {
        mDataSource.clear();
        notifyDataSetChanged();
    }

    public void removeItem(int i) {
        mDataSource.remove(i);
        notifyDataSetChanged();
    }

}
